package video.com.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class ConvertDateUtils {

    /**
     * 计算与现在与传入日期之间的相差
     * @param date 日期格式字符串
     * @return 返回相差的分钟数
     */
    public static long calTimeFromNow(String date){

        if(date == null || date.equals("")){
            return Math.round(Math.random()*10 + 1);
        }

        LocalDateTime now = LocalDateTime.now();
        long minutes = ChronoUnit.MINUTES.between(now,string2Time(date));

        return minutes;
    }

    /**
     * 时间戳转换
     * @param date
     * @return
     */
    public static long unixTimeStamp(String date){

        long time = 0L;

        if(date == null || date.equals("")){
            return time;
        }
        try{
            time = LocalDateTime.parse(date).toEpochSecond(ZoneOffset.of("+8"));
        }catch (Exception e){
            log.warn("转换异常");
            throw e;
        }

        return time;
    }

    public static LocalDateTime string2Time(String date){
        return LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE);
    }
}

